package com.controller;

import com.entity.Teacher;
import com.server.TeacherServer;
import com.util.PageBean;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TeacherController {
    @Resource
    private TeacherServer teacherService;

    @RequestMapping("addTeacher.do")
    public String addTeacher(HttpServletRequest request, Teacher teacher, HttpSession session) throws SQLException {
        Timestamp time=new Timestamp(System.currentTimeMillis());

        teacher.setAddtime(time.toString().substring(0, 19));
        teacherService.add(teacher);
        session.setAttribute("backxx", "添加成功");
        session.setAttribute("backurl", request.getHeader("Referer"));  //只有通过链接访问当前页的时候，才能获取上一页的地址
        return "redirect:postback.jsp";

    }

    @RequestMapping("doUpdateTeacher.do")
    public String doUpdateTeacher(int id, ModelMap map, Teacher teacher){   // 结果页面上需要的数据放到ModelMap对象中
        teacher = teacherService.getById(id);
        map.put("teacher", teacher);
        return "teacher_update";
    }

    //首页详细
    @RequestMapping("teacher_syDetail.do")
    public String TeacherDetail1(int id, ModelMap map, Teacher teacher){
       teacher = teacherService.getById(id);
        map.put("teacher", teacher);
        return "teacher_sy_detail";
    }
    //后台详细
    @RequestMapping("teacher_htDetail.do")
    public String TeacherDetail2(int id,ModelMap map,Teacher teacher){
        teacher = teacherService.getById(id);
        map.put("teacher", teacher);
        return "teacher_houtai_detail";
    }

    @RequestMapping("updateTeacher.do")
    public String updateTeacher(int id,ModelMap map,Teacher teacher,HttpServletRequest request,HttpSession session){
        teacherService.update(teacher);
        session.setAttribute("backxx", "修改成功");
        session.setAttribute("backurl", request.getHeader("Referer"));
        return "redirect:postback.jsp";
    }

    //分页查询
    @RequestMapping("teacher_htList.do")    //管理员
    public String TeacherList1(@RequestParam(value="page",required=false)String page,
                                    ModelMap map,HttpSession session,Teacher teacher, String t_no, String photo, String t_name, String t_sex, String t_age,String phone, String ID_number, String work_year, String salary, String note){
        if(page==null||page.equals("")){
            page="1";
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page), 5);
        Map<String, Object> pmap=new HashMap<String,Object>();
        pmap.put("pageno", pageBean.getStart());
        pmap.put("pageSize", 5);


        if(t_no==null||t_no.equals("")){pmap.put("t_no", null);}else{pmap.put("t_no",t_no);}
        if(photo==null||photo.equals("")){pmap.put("photo", null);}else{pmap.put("photo", photo);}
        if(t_name==null||t_name.equals("")){pmap.put("t_name", null);}else{pmap.put("t_name", t_name);}
        if(t_sex==null||t_sex.equals("")){pmap.put("t_sex", null);}else{pmap.put("t_sex", t_sex);}
        if(t_age==null||t_age.equals("")){pmap.put("t_age", null);}else{pmap.put("t_age",t_age);}
        if(phone==null||phone.equals("")){pmap.put("phone", null);}else{pmap.put("phone", phone);}
        if(ID_number==null||ID_number.equals("")){pmap.put("ID_number", null);}else{pmap.put("ID_number", ID_number);}
        if(work_year==null||work_year.equals("")){pmap.put("work_year", null);}else{pmap.put("work_year", work_year);}
        if(salary==null||salary.equals("")){pmap.put("salary", null);}else{pmap.put("salary", salary);}
        if(note==null||photo.equals("")){pmap.put("note", null);}else{pmap.put("note", note);}

        int total=teacherService.getCount(pmap);
        pageBean.setTotal(total);
        List<Teacher> list=teacherService.getByPage(pmap);
        map.put("page", pageBean);
        map.put("list", list);
        session.setAttribute("p", 1);
        return "teacher_houtai_list";
    }

    @RequestMapping("teacher_syList.do")   //首页
    public String TeacherList2(@RequestParam(value="page",required=false)String page,
                           ModelMap map,HttpSession session,Teacher teacher, String t_no, String photo, String t_name, String t_sex, String phone,  String work_year, String note){
        if(page==null||page.equals("")){
            page="1";
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page), 5);
        Map<String, Object> pmap=new HashMap<String,Object>();
        pmap.put("pageno", pageBean.getStart());
        pmap.put("pageSize", 5);


        if(t_no==null||t_no.equals("")){pmap.put("t_no", null);}else{pmap.put("t_no",t_no);}
        if(photo==null||photo.equals("")){pmap.put("photo", null);}else{pmap.put("photo", photo);}
        if(t_name==null||t_name.equals("")){pmap.put("t_name", null);}else{pmap.put("t_name", t_name);}
        if(t_sex==null||t_sex.equals("")){pmap.put("t_sex", null);}else{pmap.put("t_sex", t_sex);}

        if(phone==null||phone.equals("")){pmap.put("phone", null);}else{pmap.put("phone", phone);}

        if(work_year==null||work_year.equals("")){pmap.put("work_year", null);}else{pmap.put("work_year", work_year);}

        if(note==null||photo.equals("")){pmap.put("note", null);}else{pmap.put("note", note);}

        int total=teacherService.getCount(pmap);
        pageBean.setTotal(total);
        List<Teacher> list=teacherService.getByPage(pmap);
        map.put("page", pageBean);
        map.put("list", list);
        session.setAttribute("p", 1);
        return "teacher_sy_list";
    }

    @RequestMapping("deleteTeacher.do")
    public String deleteTeacher(int id,HttpServletRequest request){
        teacherService.delete(id);
        String url = request.getHeader("Referer");
        return "redirect:"+url;
    }

    @RequestMapping("quchongTeacher.do")
    public void quchongTeacher(Teacher teacher,HttpServletResponse response){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("t_no", teacher.getT_no());
        System.out.println("t_no ==="+teacher.getT_no());
        System.out.println("t_no1 ==="+teacherService.quchongTeacher(map));
        JSONObject obj=new JSONObject();
        if(teacherService.quchongTeacher(map)!=null){
            obj.put("info", "ng");
        }else{
            obj.put("info", "教师编号可以用！");

        }
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=null;
        try {
            out=response.getWriter();
            out.print(obj);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            out.close();
        }
    }



}
